package no.aina.skiforeningen;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import no.aina.xml.XMLReader;
import no.aina.xml.XMLReaderException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebView;

public class ShowTrackActivity extends Activity {

   private static final String url = "http://www.skiforeningen.no/markadb/feeds/loyper/?max=70&imgs=1&area_id=";

   private Map<Integer, Track> tracks = new HashMap<Integer, Track>();

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      int forest = IMarka.currentForest;
      int currentTrack = IMarka.currentTrack;

      if (!tracks.containsKey(currentTrack)) {

         try {
            XMLReader xml = XMLFiles.getXMLFiles().retrieveXML(url + forest);

            Element root = xml.getElement("rss");

            NodeList items = root.getElementsByTagName("item");
            Log.d("ski", "items: " + items.getLength());

            int stop = items.getLength();
            for (int i = 0; i < stop; i++) {
               Element item = (Element) items.item(i);

               String guid = xml.getNodeValue(item, "guid");
               if (guid != null) {

                  String title = xml.getNodeValue(item, "title");
                  String description = xml.getCData(item, "description").trim();

                  try {
                     // http://www.skiforeningen.no/markadb/showloype.cgi?id=11&amp;print=1
                     String link = xml.getNodeValue(item, "link");
                     URL url = new URL(link);
                     String query = url.getQuery();
                     String params[] = query.split("&");

                     int trackId = -1;
                     for (String param : params) {
                        String temp[] = param.split("=");
                        String name = temp[0];
                        if ("id".equals(name)) {
                           String id = java.net.URLDecoder.decode(temp[1], "UTF-8");
                           trackId = Integer.parseInt(id);
                        }
                     }

                     if (trackId > 0) {
                        Track track = new Track();
                        track.title = title;
                        track.description = description;
                        track.link = link;
                        tracks.put(trackId, track);
                     }

                  } catch (MalformedURLException e) {
                     e.printStackTrace();
                  } catch (UnsupportedEncodingException e) {
                     e.printStackTrace();
                  }

               }
            }

         } catch (XMLReaderException e) {
            Log.e("ski", "Error in retrieving " + url + forest);
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }

      Track t = tracks.get(currentTrack);

      WebView browser = new WebView(this);
      setContentView(browser);
//      browser.loadData("<h2>"+t.title+"</h2>" + t.description + "<br><br><a href=\""+t.link+"\">Les mer hos Skiforeningen</a>", "text/html", "iso-8859-1");
      browser.loadData("<h2>"+t.title+"</h2>" + t.description + "<br><br><a href=\""+t.link+"\">Les mer hos Skiforeningen</a>", "text/html", "utf-8");
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {

      if (keyCode == KeyEvent.KEYCODE_BACK) {
         finish();
         return true;
      }

      return super.onKeyDown(keyCode, event);
   }

   class Track {
      String title;
      String description;
      String link;
   }
}
